unit uAppConfig;

interface

uses
  SysUtils, Classes, ActiveX, uDZPersistent, uDZSerializer;

type
  TAppConfig = class(TDzPersistent)
  private
    fListenPort: Word;
    fSmsAccountName: string;
    fSmsPassword: string;
    fWorkThreadCount: Integer;
    fWhiteList: TStrings;
    procedure SetWhiteList(const Value: TStrings);
  protected
    procedure ReadProperties(const Reader: IDzClassReader;
      Options: TDzArchiveOptions); override;
    procedure WriteProperties(const Writer: IDzClassWriter;
      Options: TDzArchiveOptions); override;
  public
    constructor Create; override;
    destructor Destroy; override;
    property WhiteList: TStrings read fWhiteList write SetWhiteList;
  published
    property ListenPort: Word read fListenPort write fListenPort;
    property WorkThreadCount: Integer read fWorkThreadCount write fWorkThreadCount;
    property SmsAccountName: string read fSmsAccountName write fSmsAccountName;
    property SmsPassword: string read fSmsPassword write fSmsPassword;
  end;

var
  AppConfig: TAppConfig;

implementation

{ TAppConfig }

constructor TAppConfig.Create;
var
  ConfigFileName: string;
begin
  inherited;
  fWhiteList := TStringList.Create;
  ConfigFileName := ExtractFilePath(ParamStr(0)) + 'settings.conf';
  if FileExists(ConfigFileName) then LoadFromFile(ConfigFileName, afXML);
  if fListenPort = 0 then fListenPort := 8080;
  if WorkThreadCount = 0 then WorkThreadCount := 20;
end;

destructor TAppConfig.Destroy;
begin
  fWhiteList.Free;
  //SaveToFile(ExtractFilePath(ParamStr(0)) + 'settings.conf', afXML);
  inherited;
end;

procedure TAppConfig.ReadProperties(const Reader: IDzClassReader;
  Options: TDzArchiveOptions);
var
  IPList: string;
begin
  inherited;
  if Reader.ReadString('WhiteList', IPList) then
  begin
    TStringList(fWhiteList).Sorted := False;
    fWhiteList.CommaText := IPList;
    TStringList(fWhiteList).Sorted := True;
  end;
end;

procedure TAppConfig.SetWhiteList(const Value: TStrings);
begin
  fWhiteList.Assign(Value);
end;

procedure TAppConfig.WriteProperties(const Writer: IDzClassWriter;
  Options: TDzArchiveOptions);
begin
  inherited;
  Writer.WriteString('WhiteList', fWhiteList.CommaText);
end;

initialization
  CoInitialize(nil);
  AppConfig := TAppConfig.Create;

finalization
  AppConfig.Free;
  CoUninitialize;

end.
